
/* Generated by Interface Builder */

#import <appkit/Slider.h>
#import <appkit/Form.h>
#import <appkit/color.h>
#import <appkit/NXColorWell.h>
#import "MainController.h"
#import "ExpController.h"


@implementation MainController


- appDidInit:sender
{
    menuParms.option1 = 25.0;
    menuParms.option2 = 5.0;
    menuParms.option3 = 3.0;
    menuParms.menu_type = VERTICAL;
    menuParms.num_items = 5;

    menuParms.unselected_colour = NX_COLORLTGRAY;
    menuParms.selected_colour   = NX_COLORDKGRAY;    

    [menuView setMenuParms:&menuParms];
    [menuView setHitItem:0];
    
    return self;
}


- changeNumberItems:sender
{
    int new_value = [sender intValue];
        
    if ( new_value != menuParms.num_items )
    {
        if ( sender == numberItemsForm )
            [numberItemsSlider setIntValue:new_value];
        else
            [numberItemsForm setIntValue:new_value];
	    
        menuParms.num_items = new_value;
	
	if ( menuParms.menu_type == CIRCLE_PIE )
           [[itemOptionSliders findCellWithTag:0]
	                              setMaxValue:(360/menuParms.num_items)];

	[menuView setMenuParms:&menuParms];
    }
	
    return self;
}


- computeArea
{
    float item_area;
    
    switch ( menuParms.menu_type )
    {
        case VERTICAL:
	case CIRCLE_RECTANGLE:
	    item_area = menuParms.option1 * menuParms.option2;
	    break;
	    
	case CIRCLE_PIE:
	    item_area = ((3.14159*(menuParms.option2+menuParms.option3) * 
	                          (menuParms.option2+menuParms.option3)) -
	                 (3.14159*menuParms.option2*menuParms.option2)) /
			                           (360.0/menuParms.option1);
	    break;
	
	default:
	    fprintf(stderr, "computeArea: bad menu type\n");
	    return self;
    }
    
    [areaTextField setFloatValue:item_area];
    
    return self;
}

- changeUnselectedColour:sender
{
    menuParms.unselected_colour = [sender color];
    [menuView setMenuParms:&menuParms];
    
    return self;
}

- changeSelectedColour:sender
{
    menuParms.selected_colour = [sender color];
    [menuView setMenuParms:&menuParms];
    
    return self;
}



- changeMenuType:sender
{
    int new_type = [sender selectedTag];

    if ( new_type != menuParms.menu_type )
    {
        switch ( new_type )
	{
	    case VERTICAL:
	        [[itemOptionSliders findCellWithTag:0] setMaxValue:100];
	        [[itemOptionSliders findCellWithTag:1] setMaxValue:50];
		[[itemOptionSliders findCellWithTag:2] setMaxValue:25];
	        [itemOptionForm setTitle:"Width:" at:0];
		[itemOptionForm setTitle:"Height:" at:1];
		[itemOptionForm setTitle:"Distance:" at:2];
	        break;
		
	    case CIRCLE_PIE:
	        [[itemOptionSliders findCellWithTag:0]
	                                setMaxValue:(360/menuParms.num_items)];
	        [[itemOptionSliders findCellWithTag:1] setMaxValue:50];
		[[itemOptionSliders findCellWithTag:2] setMaxValue:50];
	        [itemOptionForm setTitle:"Angle covered:" at:0];
		[itemOptionForm setTitle:"Radius covered:" at:1];
		[itemOptionForm setTitle:"Menu radius:" at:2];
	        break;
		
	    case CIRCLE_RECTANGLE:
	        [[itemOptionSliders findCellWithTag:0] setMaxValue:100];
	        [[itemOptionSliders findCellWithTag:1] setMaxValue:100];
		[[itemOptionSliders findCellWithTag:2] setMaxValue:50];
	        [itemOptionForm setTitle:"Width:" at:0];
		[itemOptionForm setTitle:"Height:" at:1];
		[itemOptionForm setTitle:"Menu radius:" at:2];
	        break;
	}
	
        menuParms.menu_type = new_type;
	
	[menuView setMenuParms:&menuParms];
	
	[self computeArea];
    }
    
    return self;
}


- changeMenuSizing:sender
{
    float new_value = [sender floatValue];
    int   changed_option = [sender selectedTag];

    if ( sender == itemOptionForm )
        /* a field was changed, so update slider */
        [[itemOptionSliders findCellWithTag:changed_option]
	                                            setFloatValue:new_value];
    else
        /* a slider was changed, so update cell */
        [[itemOptionForm findCellWithTag:changed_option] 
	                                            setFloatValue:new_value];

    switch ( changed_option )
    {
        case 0: menuParms.option1 = new_value;
	        break;
	case 1: menuParms.option2 = new_value;
		break;
	case 2: menuParms.option3 = new_value;
		break;
    }
    
    [menuView setMenuParms:&menuParms];
    [self computeArea];

    return self;
}


- goButton:sender
{
    int num_trials;
    char *experiment_id;
    
    num_trials = [trialsField intValue];
    experiment_id = (char *) [expIDField stringValue];
    
    fprintf(stderr, "GO button hit, num_trials:%d expID:%s.\n", num_trials, experiment_id);
    
    [expController performExperiment:&menuParms :num_trials :experiment_id];
    
    return self;
}


@end
